package com.ytjj.qmyx.users.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import com.ytjj.common.exception.ApiException;
import com.ytjj.common.utils.OrderUtils;
import com.ytjj.qmyx.users.dao.UsersInfoDao;
import com.ytjj.qmyx.users.enums.UserBalanceSourceEnums;
import com.ytjj.qmyx.users.mapper.UsersWithdrawalRecordMapper;
import com.ytjj.qmyx.users.model.UserBalanceDto;
import com.ytjj.qmyx.users.model.UsersWithdrawalRecord;
import com.ytjj.qmyx.users.model.request.WithdrawalRequest;
import com.ytjj.qmyx.users.service.UserBalanceService;
import com.ytjj.qmyx.users.service.UserWithdrawalRecordService;
import com.ytjj.qmyx.users.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 *
 *  *
 **/
@Service
@RefreshScope
public class UserWithdrawalRecordServiceImpl implements UserWithdrawalRecordService {

    @Resource
    private UsersWithdrawalRecordMapper usersWithdrawalRecordMapper;

    @Resource
    private UsersInfoDao usersInfoDao;

    @Resource
    private UserBalanceService userBalanceService;
    @Value("${tixian_min_amount:100}")
    private Integer tixianMinAmount;
    @Autowired
    private UsersService usersService;

    @Override
    @Transactional
    public void add(WithdrawalRequest request) {
        BigDecimal userBrokerageNum = usersInfoDao.findUserBrokerageNum(request.getUserId());
        if(userBrokerageNum.doubleValue() < request.getAmount().doubleValue()) {
            throw new ApiException("可提现金额不足！");
        }
        if (request.getAmount().compareTo(new BigDecimal(tixianMinAmount.doubleValue())) < 0) {
            throw new ApiException("提现金额必须大于" + tixianMinAmount + "元！");
        }
        UsersWithdrawalRecord usersWithdrawalRecord = new UsersWithdrawalRecord();
        usersWithdrawalRecord.setUserId(request.getUserId());
        usersWithdrawalRecord.setOpenid(request.getOpenid());
        usersWithdrawalRecord.setAmount(request.getAmount());
        usersWithdrawalRecord.setPartnerTradeNo(OrderUtils.getWithdrawalCode(request.getUserId()));
        usersWithdrawalRecord.setDescription("");
        usersWithdrawalRecord.setStatus((byte)0);
        usersWithdrawalRecord.setCreateTime(new Date());
        usersWithdrawalRecord.setUpdateTime(new Date());
        usersWithdrawalRecordMapper.insert(usersWithdrawalRecord);

        UserBalanceDto userBalanceDto = new UserBalanceDto();
        userBalanceDto.setUserId(usersWithdrawalRecord.getUserId());
        userBalanceDto.setUpdateMoney(request.getAmount());
        userBalanceDto.setType("SUB");
        userBalanceDto.setSource("BROKERAGE");
        userBalanceDto.setAmountType(2);
        userBalanceDto.setCode(usersWithdrawalRecord.getPartnerTradeNo());

//        userBalanceService.update(userBalanceDto);
        //扣减余额
        usersService.subBalance(request.getAmount(), usersWithdrawalRecord.getUserId(), UserBalanceSourceEnums.WITHDRAWAL.name());
    }
}
